<?php

switch ($_SERVER['REQUEST_METHOD']) {
	case 'GET':
		new_course_GET();
		break;
	
	case 'POST':
		new_course_POST();
		break;
	default:
		break;
}

function new_course_GET() {
	$year = date("Y");
	?>
	<script type="text/javascript">document.body.className = document.body.className.replace('no-js','js');</script>
	<div class="wrap">
		<!--<div class="icon32" id="icon-options-general"><br></div>-->
		<h2>Add Course</h2>
		<div class='woo_breadcrumbs'>
		<a href='?page=woodle_admin'>Courses</a> &raquo; 
		New Course
		</div>
		<form method="post" action="admin.php?page=woodle_admin&action=woodle_add_course">
			<div id="titlediv">
				<div id="titlewrap">
					<div style="float:left; margin-right: 20px">
						<h3>Course Name</h3>
						<input type="text" autocomplete="off" id="title" value="" tabindex="1" size="30" name="post_title">
					</div>
					<div style="float:left;  margin-right: 20px">
						<h3>Semester</h3>
						<select name="semester">
							<option selected="selected" value="SS">Spring Semester</option>
							<option value="FS">Fall Semester</option>
						</select>	
					</div>
					<div style="float:left">
						<h3>Year</h3>
						<select name="year">
							<option selected="selected" value="<?php echo $year ?>"><?php echo $year ?></option>
							<?php
							for($i=0;$i<20;$i++){
								$year++;
								echo "<option value=".$year.">".$year."</option>";
							}
							?>
						</select>
					</div>
				</div>
			</div>
			<div style="clear:both; padding-top: 20px">
				<h3>Course Description</h3>
				<div class="form-table">
					<?php
						$args = array("textarea_name" => "wpet_options[textarea_one]");
						wp_editor( $options['textarea_one'], "wpet_options[textarea_one]", $args );
					?>
				</div>
			</div>
			<p class="submit">
			<input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
			</p>
		</form>
	</div>
	<?php	
}

function new_course_POST(){
	if(isset($_GET['action']) && strlen(preg_replace('/\s+/u','',$_POST['post_title'])) != 0){
		?><div class="updated below-h2" id="message"><p>Course added</p></div><?php
		global $woodb;
		$woodb->createCourse($_POST['post_title'], $_POST['wpet_options']['textarea_one'], $_POST['semester'], intval($_POST['year']));
	} else {
		?><div class="updated below-h2" id="message"><p>Invalid inputs</p></div><?php
	}
}

?>